package com.sam;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;


public class AlertBox extends Activity {

	SocketService mBoundService;
	boolean mIsBound = false;
	AlertDialog alertDialog;
	AlertBox obj;

	private ServiceConnection mConnection = new ServiceConnection() {
		// EDITED PART
		@SuppressWarnings("deprecation")
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mBoundService = ((SocketService.LocalBinder) service).getService();
			
			
			alertDialog.setButton("Attend", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Add your code for the button here.
					/*Log.i("sessionId in box",mBoundService.getSessionId());
					/*if(mBoundService.getSessionId().equals("none")){
						//mBoundService.getTCPClient().sendMessage(new DataStream("attend",null,null));
						LoginActivity.la.finish();
						Intent intent = new Intent(obj,LoginActivity.class);
						String[] a = new String[1];
						a[0]="go";
						intent.putExtra("parameters",a);
						startActivity(intent);
						
					}
					else{*/
						
						mBoundService.getTCPClient().sendMessage(new DataStream("attend",null,null));
						
					   
					//}
				}
			});
			
			alertDialog.setButton2("Reject", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Add your code for the button here.
					
					mBoundService.getTCPClient().sendMessage(new DataStream("rejected",null,null));
					
				}
			});
			
			alertDialog.show();

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBoundService = null;
		}

	};
	
	private void doBindService() {
		bindService(new Intent(this, SocketService.class), mConnection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
		if (mBoundService != null) {
			mBoundService.IsBoundable();
		}
	}

	private void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(mConnection);
			mIsBound = false;
		}
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("SaM Threat Alert");
		alertDialog.setMessage("This is a threat alert call");
        obj = this;
		// Set the Icon for the Dialog
		alertDialog.setIcon(R.drawable.sam_launcher);
		
		doBindService();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
}
